<?php

namespace app\admin\model;

use app\common\enums\StatusEnum;

class LangClass extends \app\common\model\LangClass
{
    /**
     * 添加语种显示的班级
     * @param $langId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getListByLang($langId)
    {
        if ($langId > 0) {
            $model = static::field('id, lang_id, type, money, content')
                ->where(['lang_id' => $langId, 'status' => StatusEnum::ENABLED])
                ->select()
                ->toArray();
            $result = [];
            foreach ($model as $key => $value) {
                $value['typeName'] = self::attributes('type', $value['type']);
                $result[$value['type']] = $value;
            }
        } else {
            $result = [
                self::CLASS_FIRST => [
                    'id' => 0,
                    'lang_id' => $langId,
                    'type' => self::CLASS_FIRST,
                    'typeName' => self::attributes('type', self::CLASS_FIRST),
                    'money' => '',
                    'content' => '',
                ],
                self::CLASS_SECOND => [
                    'id' => 0,
                    'lang_id' => $langId,
                    'type' => self::CLASS_SECOND,
                    'typeName' => self::attributes('type', self::CLASS_SECOND),
                    'money' => '',
                    'content' => '',
                ],
                self::CLASS_THIRD => [
                    'id' => 0,
                    'lang_id' => $langId,
                    'type' => self::CLASS_THIRD,
                    'typeName' => self::attributes('type', self::CLASS_THIRD),
                    'money' => '',
                    'content' => '',
                ],
            ];
        }
        return $result;
    }

    /**
     * 语种列表显示的班级信息
     * @param $langIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getColumn($langIds)
    {
        $model = static::field('id, lang_id, type, money')
            ->where('lang_id', 'in', $langIds)
            ->where(['status' => StatusEnum::ENABLED])
            ->select()
            ->toArray();
        $result = [];
        foreach ($model as $key => $value) {
            $result[$value['lang_id']][$value['type']] = $value;
        }
        return $result;
    }

    /**
     * 添加修改语种班级
     * @param $langId
     * @param $data
     * @throws \Exception
     */
    public static function saveOne($langId, $data)
    {
        $model = new LangClass();
        $class = [];
        foreach ($data as $type => $value) {
            $class[$type] = [
                'lang_id' => $langId,
                'type' => $type,
                'money' => $value['money'],
                'content' => $value['content'],
                'status' => StatusEnum::ENABLED,
            ];
            if ($value['id'] > 0) {
                $class[$type]['id'] = $value['id'];
            }
        }
        if ($class) {
            $model->saveAll($class);
        }
    }

    public static function getClassByType($langId, $typeIds)
    {
        return static::field('id, type')
            ->where(['lang_id' => $langId, 'status' => StatusEnum::ENABLED])
            ->where('type', 'in', $typeIds)
            ->column('type', 'id');
    }
}